package com.party.admin.web.controller.order;

import com.party.admin.biz.order.OrderBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.admin.web.dto.output.order.OrderFormOutput;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.core.model.order.OrderFormHistory;
import com.party.core.model.order.OrderType;
import com.party.core.service.activity.OrderActivityBizService;
import com.party.core.service.order.IOrderFormHistoryService;
import com.party.core.service.order.IOrderFormService;
import com.party.core.utils.MyBeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author yifeng
 * @createTime 2018/1/25 0025 上午 11:34
 * @description 历史订单控制器
 */
@Controller
@RequestMapping("order/orderHistory")
public class OrderFormHistoryController {
    @Autowired
    private IOrderFormHistoryService orderFormHistoryService;
    @Autowired
    private OrderActivityBizService orderActivityBizService;
    @Autowired
    private IOrderFormService orderFormService;
    @Autowired
    private OrderBizService orderBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 订单列表
     *
     * @param orderFormHistory
     * @param tradeStatus
     * @param page
     * @param commonInput
     * @return
     */
    @RequestMapping(value = "orderList")
    public ModelAndView orderList(OrderFormHistory orderFormHistory, String tradeStatus, String initiatorName, Page page, CommonInput commonInput) {
        ModelAndView mv = new ModelAndView("order/orderHistoryList");
        commonInput.setIsDate(1);
        Map<String, Object> params = CommonInput.appendParams(commonInput);

        mv.addObject("input", commonInput);

        params.put("initiatorName", initiatorName);
        mv.addObject("initiatorName", initiatorName);
        page.setLimit(20);
        Map<String, Object> pageParams = CommonInput.appendPageParams(page);
        params.putAll(pageParams);

        mv.addObject("tradeStatus", tradeStatus);

        Integer totalCount = orderFormHistoryService.newWebListCount(orderFormHistory, params);
        page.setTotalCount(totalCount);

        List<OrderFormHistory> orderForms = orderFormHistoryService.newWebListPage(orderFormHistory, params);
        List<OrderFormOutput> orderFormOutputs = LangUtils.transform(orderForms, input -> {
            Integer paymentWay = input.getPaymentWay();
            OrderFormOutput orderFormOutput = new OrderFormOutput();
            try {
                MyBeanUtils.copyBeanNotNull2Bean(input, orderFormOutput);
            } catch (Exception e) {
                e.printStackTrace();
            }
            orderFormOutput.setPaymentWay(paymentWay);

            // 获取商户名称
            String merchantName = orderActivityBizService.
                    getMerchantName(input.getMerchantId(), input.getPaymentWay(), input.getInitiatorId(), input.getMerchantName());
            orderFormOutput.setMerchantName(merchantName);

            return orderFormOutput;
        });

        mv.addObject("page", page);
        mv.addObject("orderForms", orderFormOutputs);

        Map<Integer, String> orderTypes = OrderType.convertMap();
        mv.addObject("orderTypes", orderTypes);

        mv.addObject("orderForm", orderFormHistory);
        return mv;
    }

    /**
     * 将超时订单状态改为其他
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("updateOvertimeOrderCancel")
    public AjaxResult updateOvertimeOrderCancel() {
        Integer updateCount = orderBizService.updateOvertimeOrderCancel();
        return AjaxResult.success((Object) ("更新成功数量：" + updateCount));
    }

    /**
     * 复制和删除已取消订单
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("copyAndDeleteCanceledOrder")
    public AjaxResult copyAndDeleteCanceledOrder() {
        boolean successFlag = orderBizService.copyAndDeleteCanceledOrder();
        return AjaxResult.success(successFlag);
    }

    /**
     * 恢复历史订单到订单表
     *
     * @param orderId 目标订单id
     * @return
     */
    @ResponseBody
    @RequestMapping("recoveryHistoryOrder")
    public AjaxResult recoveryHistoryOrder(String orderId) {
        boolean successFlag = orderBizService.recoveryHistoryOrder(orderId);
        if (successFlag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("恢复订单异常");
        }
    }
}
